// Copyright (c) 2014 Jonathan Magnan (http://jonathanmagnan.com).
// All rights reserved (http://jonathanmagnan.com/extension-methods-library).
// Licensed under MIT License (MIT) (http://zextensionmethods.codeplex.com/license).

using System.Net;

#if EXTENSIONMETHODS_WITHNAMESPACE
namespace JHSoft.Extension.ExtensionMethods
{
#endif
#if EXTENSIONMETHODS_INTERNAL
    internal static class WebRequestExtensions
#else
    public static class WebRequestExtensions
#endif
    {
        /// <summary>
        ///     A WebRequest extension method that gets the WebRequest response or the WebException response.
        /// </summary>
        /// <param name="this">The @this to act on.</param>
        /// <returns>The WebRequest response or WebException response.</returns>
        /// <example>
        ///     <code>
        ///           using System.Net;
        ///           using Microsoft.VisualStudio.TestTools.UnitTesting;
        ///           using JHSoft.Extension.ExtensionMethods;
        ///           
        ///           namespace ExtensionMethods.Examples
        ///           {
        ///               [TestClass]
        ///               public class System_Net_WebRequest_GetResponseSafe
        ///               {
        ///                   [TestMethod]
        ///                   public void GetResponseSafe()
        ///                   {
        ///                       // Type
        ///                       WebRequest @this = WebRequest.Create(&quot;http://www.zzzportal.com/&quot;);
        ///           
        ///                       // Examples
        ///                       WebResponse value = @this.GetResponseSafe(); // return a response;
        ///           
        ///                       // Unit Test
        ///                       Assert.IsNotNull(value);
        ///                   }
        ///               }
        ///           }
        ///     </code>
        /// </example>
        public static WebResponse GetResponseSafe(this WebRequest @this)
        {
            try
            {
                return @this.GetResponse();
            }
            catch (WebException e)
            {
                return e.Response;
            }
        }
    }
#if EXTENSIONMETHODS_WITHNAMESPACE
}
#endif